<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="../cwjy/css/app.css" rel="stylesheet">
    <style>
        html::-webkit-scrollbar {
            display: none
        }
    </style>
</head>
<script src="../cwjy/js/app.js"></script>
<script src="../cwjy/js/jquery.min.js"></script>
<body>
<div class="card">
    <div class="card-header">
        <div>
            <div style="float:left;">
                <span style="font-weight: bold;font-size: 20px;text-shadow: 1px 1px 2px #000000;">商品类别</span>
            </div>
            <div style="float: right;margin-right: 7%">
                <button type="button"  data-feather="plus-circle" data-toggle="modal"
                        data-target="#defaultModalPrimary" onclick="initMode('addData()')"></button>
            </div>
        </div>
        <table class="table table-striped" cellpadding="0" cellspacing="0">
            <thead>
            <tr>
                <th style="width:20%;">序号</th>
                <th style="width:20%;">类别名称</th>
                <th style="width:25%">创建人</th>
                <th class="d-none d-md-table-cell" style="width:25%">创建时间</th>
                <th>Actions</th>
            </tr>
            </thead>
            <tbody id = "tbodyDiv">

            </tbody>
        </table>
    </div>
</div>

<!--模态框-->

<div class="modal fade" id="defaultModalPrimary" tabindex="-1" role="dialog" aria-hidden="true" >
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header" id="modelHead">

            </div>
            <div class="modal-body m-3">
                <div class="card-body" id="modelDiv">

                </div>
            </div>
            <div class="modal-footer" id="modelAction">

            </div>
        </div>
    </div>
</div>
<script type="text/javascript" src="../cwjy/js/ajaxAll.js"></script>

<script type="text/javascript">


    let categoryData = [];

    function getPage() {
        let url = "http://localhost:8888/cwjy/CategoryDetails/getPage";
        let json = {};
        $.ajax({
            type: 'post',
            async: false,            //异步请求（同步请求将会锁住浏览器，用户其他操作必须等待请求完成才可以执行）
            url: url,    //请求发送到Servlet
            contentType: 'application/json;charset=UTF-8',//指定消息请求类型
            data: JSON.stringify(json),
            timeout: 10000,    //超时10s
            success: function (res) { returnLogin(res);
                categoryData = res.data
                forEachData();
            },
            error: function (errorMsg) {
                //请求失败时执行该函数
                alert('数据请求失败!');
            }
        });
    }
    getPage();

    function forEachData(){
        let tbodyDiv = "";
        for (let i = 0; i < categoryData.length; i++) {
            let temp = "    <tr>\n" +
                "                <td>"+( i + 1 )+"</td>\n" +
                "                <td>"+categoryData[i].classification_name+"</td>\n" +
                "                <td>"+categoryData[i].create_user+"</td>\n" +
                "                <td class=\"d-none d-md-table-cell\">"+categoryData[i].create_date+"</td>\n" +
                "                <td class=\"table-action\">\n" +
                "                    <a href=\"#\"><i class=\"align-middle\" data-feather=\"eye\" onclick=\"initModeViewDetails('"+categoryData[i].id+"')\"\n" +
                "                                   data-toggle=\"modal\" data-target=\"#defaultModalPrimary\"></i></a>\n" +
                "                    <a href=\"#\"><i class=\"align-middle\" data-feather=\"edit-2\" onclick=\"updateText('"+categoryData[i].id+"')\" data-toggle=\"modal\"\n" +
                "                                   data-target=\"#defaultModalPrimary\"></i></a>\n" +
                "                    <a href=\"#\"><i class=\"align-middle\" data-feather=\"trash\" onclick=\"deleteData('"+categoryData[i].id+"')\"></i></a>\n" +
                "                </td>\n" +
                "            </tr>";
            tbodyDiv += temp;
        }
        $('#tbodyDiv').html(tbodyDiv)
    }


    function addType() {
        addNodeMessage("")
        feather.replace();
        initClassificationDetailedClick();
    }

    function addNodeMessage(name) {
        let temp = "    <div class=\"col-md-12\" >\n" +
            "                                    <div class=\"col-md-11\" style=\"float: left\">\n" +
            "                                        <input type=\"text\" class=\"form-control \"\n" +
            "                                               placeholder=\"类型名称\" value='" + name + "' style=\"margin-bottom: 10px\">\n" +
            "                                    </div>\n" +
            "                                    <div class=\"col-md-1\" style=\"float: left\">\n" +
            "                                   <span>     <button type=\"button\" data-feather=\"x-circle\"\n" +
            "                                                style=\"float: right;margin-top: 15%;color: red\"></button>\n" +
            "                                    </div></span>\n" +
            "                                </div>";
        $('#classification_detailed').append(temp)
    }

    function initClassificationDetailedClick() {
        $('#classification_detailed span').unbind("click")
        $('#classification_detailed span').click(function () {
            if (confirm('是否删除该节点?')) {
                $(this).parent().parent().remove()
            }
        })
    }

    initClassificationDetailedClick();

    function addData() {

        let type_name_array = [];
        let classification_name = $('#classification_name');
        let type_name = $('#classification_detailed')
        type_name.find("input").each(function (i) {
            type_name_array[i] = $(this).val();
        })
        let url = "http://localhost:8888/cwjy/CategoryDetails/insert";
        let json = {"classification_name": classification_name.val(), "commodity_category": type_name_array};
        let success = "增加成功！";
        let error = "增加失败";
        ajaxUnifiedCallUpdateAndAdd(url, json, success, error)
    }

    function initMode(onType) {

        let modiv = "<div class=\"mb-3 col-md-12\" style='font-weight: bold;text-shadow: 1px 1px 3px #000000;margin-top: -6%'>\n" +
            "                            <label class=\"form-label\" for=\"classification_name\">类别名称</label>\n" +
            "                            <input type=\"email\" class=\"form-control\" id=\"classification_name\" placeholder=\"类别名称\">\n" +
            "                        </div>\n" +
            "                        <div class=\"mb-3  col-md-12\" style='font-weight: bold;text-shadow: 1px 1px 3px #000000;'>\n" +
            "                            <label class=\"form-label\">类型名称</label>\n" +
            "                            <button type=\"button\" class=\"form-label\" for=\"type_name\" data-feather=\"plus-circle\"\n" +
            "                                    style=\"float: right\" onclick=\"addType()\"></button>\n" +
            "                            <div id=\"classification_detailed\">\n" +
            "                                <div class=\"col-md-12\">\n" +
            "                                    <div class=\"col-md-11\" style=\"float: left\">\n" +
            "                                        <input type=\"text\" class=\"form-control \"\n" +
            "                                               placeholder=\"类型名称\" style=\"margin-bottom: 10px\">\n" +
            "                                    </div>\n" +
            "                                    <div class=\"col-md-1\" style=\"float: left\">\n" +
            "                                        <span>\n" +
            "                                        <button type=\"button\" data-feather=\"x-circle\"\n" +
            "                                                style=\"float: right;margin-top: 15%;color: red\"\n" +
            "                                        ></button>\n" +
            "                                            </span>\n" +
            "                                    </div>\n" +
            "                                </div>\n" +
            "                            </div>\n" +
            "                        </div>";
        $('#modelDiv').html(modiv)

        let modelHead = " <h3 class=\"modal-title\" id='modelHeadText' style=\"font-weight: bold;text-shadow: 1px 1px 3px #000000;\">类别增加</h3>\n" +
            "                <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-label=\"Close\">\n" +
            "                    <span aria-hidden=\"true\">&times;</span>\n" +
            "                </button>";

        $('#modelHead').html(modelHead)

        let modelAction = " <button type=\"button\" class=\"btn btn-secondary\" data-dismiss=\"modal\">关闭</button>\n" +
            "                <button type=\"button\" class=\"btn btn-primary\" onclick=\"" + onType + "\">提交</button>";
        $('#modelAction').html(modelAction)
        feather.replace();
    }


    function initModeViewDetails(id) {
        let data = getDataIdData(categoryData,id)
        let modelHead = "<h3 class=\"modal-title\" style='font-weight: bold;text-shadow: 1px 1px 3px #000000;'>类别详细</h3>\n" +
            "                   <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-label=\"Close\">\n" +
            "                       <span aria-hidden=\"true\">&times;</span>\n" +
            "                   </button>";

        let typeDetailed = "";
        for (let i = 0; i < data.commodity_category.length; i++) {
            let temp = "<p class=\"text-muted btn btn-outline-secondary\" style='margin-right: 10px'>"+data.commodity_category[i].type_name+"</p>";
            typeDetailed += temp;
        }
        let modelDiv = " <div  style='margin-top: -6%;'>  <h1 style='font-weight: bold;text-shadow: 1px 1px 3px #000000;'>类别名称</h1>\n" +
            "                    <p class=\"text-muted btn btn-outline-secondary\">"+data.classification_name+"</p>\n" +
            "                    <h1 style='font-weight: bold;text-shadow: 1px 1px 3px #000000;'>类型详细</h1>\n" +
            ""+typeDetailed+"</div>";
        let modelAction = "  <button type=\"button\" class=\"btn btn-secondary\" data-dismiss=\"modal\">Close</button>";
        $('#modelHead').html(modelHead)
        $('#modelDiv').html(modelDiv)
        $('#modelAction').html(modelAction)

    }

    function updateText(id) {
        let data = getDataIdData(categoryData,id)
        initMode('updateData(\'' + id + '\')');
        $('#classification_detailed').html("")
        $('#modelHeadText').html("类型修改")
        $('#classification_name').val(data.classification_name)
        for (let i = 0; i < data.commodity_category.length; i++) {
            addNodeMessage(data.commodity_category[i].type_name);
        }
        initClassificationDetailedClick();
        feather.replace();
    }

    function updateData(id) {
        let type_name_array = [];
        let classification_name = $('#classification_name');
        let type_name = $('#classification_detailed')
        type_name.find("input").each(function (i) {
            type_name_array[i] = $(this).val();
        })
        let url = "http://localhost:8888/cwjy/CategoryDetails/updateByPrimaryKeySelective";
        let json = {"classification_name": classification_name.val(), "commodity_category": type_name_array,"id":id};
        let success = "修改成功！";
        let error = "修改失败";
        if(confirm("是否进行修改！")) {
            ajaxUnifiedCallUpdateAndAdd(url, json, success, error)
        }
    }

    function deleteData(id) {
        let url = "http://localhost:8888/cwjy/CategoryDetails/deleteByPrimaryKey";
        let json = {"id":id};
        let success = "删除成功！";
        let error = "删除失败";
        if(confirm("是否进行删除！")){
            ajaxUnifiedCallUpdateAndAdd(url,json,success,error)
        }
    }





</script>

</body>
</html>